package cn.kust.serviceusercenter.service.impl;

import cn.kust.microtourismcommon.constant.CommonStatusEnum;
import cn.kust.microtourismcommon.constant.IdentityConstants;
import cn.kust.microtourismcommon.pojo.dto.MicroTourismUser;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.microtourismcommon.pojo.dto.UserNameAvatar;


import cn.kust.serviceusercenter.mapper.UserBaseMapper;
import cn.kust.serviceusercenter.mapper.UserMapper;
import cn.kust.serviceusercenter.service.IUserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WY
 * @data 2022/7/20 16:59
 **/
@Service
@Slf4j
public class IUserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserBaseMapper userBaseMapper;


    @Override
    public ResponseResult<MicroTourismUser> loginOrRegister(String userPhone) {
        // 查询用户信息
        Map<String ,Object> map=new HashMap<>();
        map.put("user_phone",userPhone);
        List<MicroTourismUser> microTourismUsers = userMapper.selectByMap(map);
        log.debug(microTourismUsers.size()==0?"没有该用户,执行注册功能": microTourismUsers.get(0).getUserPhone());

        // 判断用户信息是否存在
        if (microTourismUsers.size()== 0 ){
            MicroTourismUser microTourismUser = new MicroTourismUser();
            microTourismUser.setUserName(IdentityConstants.USER_NEWNAME);
            microTourismUser.setUserGender((byte) 0);
            microTourismUser.setUserPhone(userPhone);
            microTourismUser.setUserState((byte) 0);
            LocalDateTime now = LocalDateTime.now();
            microTourismUser.setCreatedTime(now);
            microTourismUser.setUpdatedTime(now);
            userMapper.insert(microTourismUser);
            microTourismUsers = userMapper.selectByMap(map);
        }
//        从list中取出来的数据需要进行转化成json格式字符串，然后再将该json格式字符串转换成对象，这样就不会再出现报错情况，能成功遍历该list列表。
        String JO = JSON.toJSONString(microTourismUsers.get(0));
        MicroTourismUser microTourismUser = JSONObject.parseObject(JO, MicroTourismUser.class);
        return ResponseResult.success(microTourismUser);
    }

    @Override
    public ResponseResult getUserByPhone(String userPhone) {
        Map<String ,Object> map=new HashMap<>();
        map.put("user_phone",userPhone);
        List<MicroTourismUser> passengerMicroTourismUsers =userMapper.selectByMap(map);

        if (passengerMicroTourismUsers.size() == 0){
            return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXIST.getCode(),CommonStatusEnum.USER_NOT_EXIST.getValue());
        }else {
            MicroTourismUser microTourismUser = passengerMicroTourismUsers.get(0);
            return ResponseResult.success(microTourismUser);
        }
    }

    @Override
    public ResponseResult<UserNameAvatar> getUserNameAvatar(String userId) {
        QueryWrapper<UserNameAvatar> wrapper = new QueryWrapper();
        long userid = Long.parseLong(userId);
        wrapper.select("id as userId","user_name","avatar").eq("id",userid);
        UserNameAvatar userNameAvatar = userBaseMapper.selectOne(wrapper);
//        UserNameAvatar userNameAvatar = new UserNameAvatar();
//        userNameAvatar.setUserId(1L);
//        userNameAvatar.setUserName("用户");
//        userNameAvatar.setAvatar("头像");

        return ResponseResult.success(userNameAvatar);
    }

    @Override
    public ResponseResult getUserNameAvatarListById(List<Long> userIds) {

        QueryWrapper<UserNameAvatar> wrapper = new QueryWrapper();
        wrapper.select("id as userId","user_name","avatar").in("id",userIds);
        List<UserNameAvatar> resultList = userBaseMapper.selectList(wrapper);

        return ResponseResult.success(resultList);
    }

    @Override
    public ResponseResult<List<MicroTourismUser>> getUsersPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<MicroTourismUser> userList =  userMapper.selectList(new QueryWrapper<>());
        for (MicroTourismUser user: userList
             ) {
            System.out.println(user);
        }

        return ResponseResult.success(userList);
    }

}
